//
//  MineTool.m
//  Mine
//
//  Created by CPX on 2018/7/23.
//  Copyright © 2018年 CPX. All rights reserved.
//

#import "MineTool.h"

#define UserName @"username"
#define Email @"email"
#define UserToken @"token"
#define Avatar @"avatar"
#define IsLogin @"islogin"

#define DeviceID @"device_id"

#define ASR @"setting_asr"
#define HighLatitude @"setting_highLatitude"
#define KeyPrayTimeCalMeth @"praytimecalmeth"

/**
 我的模块设置相关
 @author yzl
 */
@interface MineTool()

@property (nonatomic,strong) NSUserDefaults *defaults;
@end

@implementation MineTool

+ (instancetype)sharedTool{
    static dispatch_once_t onceToken;
    static MineTool *tool = nil;
    dispatch_once(&onceToken, ^{
        tool = [MineTool new];
        [tool _init];
    });
    return tool;
}

- (void)_init{
    self.defaults = [NSUserDefaults standardUserDefaults];
    
    self.defaults = [NSUserDefaults standardUserDefaults];
    id asr = [self.defaults objectForKey:ASR];
    self.asr = asr?[asr integerValue]:0;
    
    id highL = [self.defaults objectForKey:HighLatitude];
    self.highLatitude = highL?[highL integerValue]:1;
    
    id prayTimeC = [self.defaults objectForKey:KeyPrayTimeCalMeth];
    self.prayTimeCalMeth = prayTimeC?[prayTimeC integerValue]:3;
    
    self.username = [self.defaults objectForKey:UserName];
    self.username = self.username?self.username:@"";
    
    self.userToken = [self.defaults objectForKey:UserToken];
    self.userToken = self.userToken?self.userToken:@"";
    
    self.email = [self.defaults objectForKey:Email];
    self.email = self.email?self.email:@"";
    
    self.avatar = [self.defaults objectForKey:Avatar];
    self.avatar = self.avatar?self.avatar:@"";
    
    id isLogin = [self.defaults objectForKey:IsLogin];
    self.isLogin = [isLogin boolValue];
    
    self.deviceId = [self.defaults stringForKey:DeviceID];
    if (!self.deviceId) {
        self.deviceId = [self getUUID];
        [self.defaults setObject:self.deviceId forKey:DeviceID];
    }
    
    [AppDataManager sharedManager].userToken = self.userToken;
    [AppDataManager sharedManager].email = self.email;
    [AppDataManager sharedManager].username = self.username;
    [AppDataManager sharedManager].avatar = self.avatar;
    [AppDataManager sharedManager].isLogin = self.isLogin;
    [BaseApi standard].auth = [NSString stringWithFormat:@"Token %@",self.userToken];
}

- (NSString *)getUUID {
    CFUUIDRef puuid = CFUUIDCreate( nil );
    CFStringRef uuidString = CFUUIDCreateString(nil, puuid);
    NSString *result = (NSString *)CFBridgingRelease(CFStringCreateCopy( NULL, uuidString));
    return result;
}

- (void)saveUserInfo{
    if ([self.avatar isKindOfClass:[NSNull class]] || self.avatar == nil) {
        self.avatar = @"";
    }
    if ([self.email isKindOfClass:[NSNull class]] || self.email == nil) {
        self.email = @"";
    }
    [self.defaults setObject:self.userToken forKey:UserToken];
    [self.defaults setObject:self.username forKey:UserName];
    [self.defaults setObject:self.email forKey:Email];
    [self.defaults setObject:self.avatar forKey:Avatar];
    [self.defaults setBool:self.isLogin forKey:IsLogin];
    
    [AppDataManager sharedManager].userToken = self.userToken;
    [AppDataManager sharedManager].email = self.email;
    [AppDataManager sharedManager].username = self.username;
    [AppDataManager sharedManager].avatar = self.avatar;
    [AppDataManager sharedManager].isLogin = self.isLogin;
    
    [BaseApi standard].auth = [NSString stringWithFormat:@"Token %@",self.userToken];
}

- (void)clearUserInfo{
    self.username = @"";
    self.userToken = @"";
    self.email = @"";
    self.isLogin = NO;
    self.avatar = @"";
    [self saveUserInfo];
}


- (void)saveAsr:(NSInteger)asr{
    self.asr = asr;
    [self.defaults setInteger:self.asr forKey:ASR];
    [AppDataManager sharedManager].asr = self.asr;
}

- (void)saveHighLatitude:(NSInteger)highLatitude{
    self.highLatitude = highLatitude;
    [self.defaults setInteger:self.highLatitude forKey:HighLatitude];
    [AppDataManager sharedManager].highLatitude = self.highLatitude;
}

- (void)savePrayTimeCalMeth:(NSInteger)prayTimeCalMeth{
    self.prayTimeCalMeth = prayTimeCalMeth;
    [self.defaults setInteger:self.prayTimeCalMeth forKey:KeyPrayTimeCalMeth];
    [AppDataManager sharedManager].prayTimeCalMeth = self.prayTimeCalMeth;
}

- (void)registerToken{
    
}

@end
